
const API_KEY = 'c6e42987e00aed531fa7f1a48c936ad7'
const BASE_URL = 'https://restapi.amap.com/v3/geocode/regeo'

/**
 * 根据经纬度解析成 省市区街道四级地址
 */
export const reginDetail = async (longitude : any, latitude : any) => {

  try {

    const pointUrl = `${BASE_URL}?location=${longitude},${latitude}&key=${API_KEY}&radius=1000&extensions=all`

    const response : any = await uni.request({
      url: pointUrl,
      method: 'GET',
    })
    const { data } = response
    if (!data.regeocode || !data?.regeocode?.addressComponent) {
      return null
    }
    const { province, city, district, township } = data.regeocode.addressComponent

    const addressDetail = {
      province,
      city,
      district,
      township,
      address: data.regeocode.formatted_address
    }

    return addressDetail
  } catch (error) {
    console.error('Error processing address component:', error)
    return null
  }
}
